package com.frinder.robot.wx;

import com.frinder.base.akka.BaseActor;
import com.frinder.base.akka.MyLookupEventBus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Component;

/**
 * @ClassName MapMessageActor
 * @Description 地图消息处理器
 * @Author frinder
 * @Date 2019/9/30 13:47
 * @Verion 1.0
 */
@Component
@Scope("prototype")
public class MapMessageActor extends BaseActor {

    public static final String NAME = "map-message-actor";

    @Autowired
    private SimpMessagingTemplate template;

    @Override
    public void preStart() throws Exception {
        super.preStart();
        MyLookupEventBus.BUS.subscribe(getSelf(), MyLookupEventBus.Topic.MAP_MESSAGE_EVENT);
    }

    @Override
    public Receive createReceive() {
        return receiveBuilder()
                .match(MapMessageEvent.class, this::push)
                .build();
    }

    private void push(MapMessageEvent event) {
        blog.info("推送地图消息：{}", event);
        template.convertAndSend("/topic/map", event.getMessage());
    }
}
